﻿Public Class frmAdjust
    Public IsDraft As Boolean = False

    Public g As Graphics

    Public b1 As SolidBrush
    Public b2 As SolidBrush
    Public b3 As SolidBrush
    Public b4 As SolidBrush
    Public bCenter As SolidBrush

    Public isAreaInfoChanged As Boolean = False

    Public isInited As Boolean = False

    Public m_AreaInfoPhase1 As frmMain.AreaInfo
    Public m_AreaInfoPhase2 As frmMain.AreaInfo
    Public m_AreaInfoPhase3 As frmMain.AreaInfo
    Public m_AreaInfoPhase4 As frmMain.AreaInfo
    Public m_AreaInfoPhaseCenter As frmMain.AreaInfo

    Public SamplePicSize As Size
    Public ScaleX As Double = 1.0
    Public ScaleY As Double = 1.0

    Public startPoint As Point
    Public endPoint As Point

    Public Structure strScaleInfo
        Dim scaleX As Double
        Dim scaleY As Double
    End Structure

    Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If Not isInited Then
            LoadSamplePicture()

            b1 = New SolidBrush(Color.FromArgb(90, radSetAreaPhase1.BackColor))
            b2 = New SolidBrush(Color.FromArgb(90, radSetAreaPhase2.BackColor))
            b3 = New SolidBrush(Color.FromArgb(90, radSetAreaPhase3.BackColor))
            b4 = New SolidBrush(Color.FromArgb(90, radSetAreaPhase4.BackColor))
            bCenter = New SolidBrush(Color.FromArgb(90, radSetAreaPhaseCenter.BackColor))

            m_AreaInfoPhase1.StartPoint = changePointScale(frmMain.areaPhase1.StartPoint, 1)
            m_AreaInfoPhase1.AreaSize = changePointScale(frmMain.areaPhase1.AreaSize, 1)

            m_AreaInfoPhase2.StartPoint = changePointScale(frmMain.areaPhase2.StartPoint, 1)
            m_AreaInfoPhase2.AreaSize = changePointScale(frmMain.areaPhase2.AreaSize, 1)

            m_AreaInfoPhase3.StartPoint = changePointScale(frmMain.areaPhase3.StartPoint, 1)
            m_AreaInfoPhase3.AreaSize = changePointScale(frmMain.areaPhase3.AreaSize, 1)

            m_AreaInfoPhase4.StartPoint = changePointScale(frmMain.areaPhase4.StartPoint, 1)
            m_AreaInfoPhase4.AreaSize = changePointScale(frmMain.areaPhase4.AreaSize, 1)

            m_AreaInfoPhaseCenter.StartPoint = changePointScale(frmMain.areaPhaseCenter.StartPoint, 1)
            m_AreaInfoPhaseCenter.AreaSize = changePointScale(frmMain.areaPhaseCenter.AreaSize, 1)

            reDraw()

            isInited = True
        End If
    End Sub


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        g = picboxSamplePic.CreateGraphics


        'reDraw()

    End Sub

    Public Function LoadSamplePicture() As Boolean
        LoadSamplePicture = False
        Dim bmp As Bitmap

        Try
            If frmMain.picboxMain.Image Is Nothing Then
                bmp = New Bitmap("samplePic.jpg")
            Else
                bmp = frmMain.picboxMain.Image.Clone
            End If

            SamplePicSize = New Size(bmp.Size)

            ScaleX = picboxSamplePic.Width / SamplePicSize.Width
            ScaleY = picboxSamplePic.Height / SamplePicSize.Height

            picboxSamplePic.Image = bmp

        Catch ex As Exception

        End Try
    End Function

    Private Sub Form2_UNLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
        g.Dispose()
        'p.Dispose()
        'p.Dispose()

        b1.Dispose()
        b2.Dispose()
        b3.Dispose()
        b4.Dispose()
        bCenter.Dispose()

        If isAreaInfoChanged Then

            If MsgBox("是否保存区域参数？", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then

                frmMain.areaPhase1.StartPoint = changePointScale(m_AreaInfoPhase1.StartPoint)
                frmMain.areaPhase1.AreaSize = changeSizeScale(m_AreaInfoPhase1.AreaSize)

                frmMain.areaPhase2.StartPoint = changePointScale(m_AreaInfoPhase2.StartPoint)
                frmMain.areaPhase2.AreaSize = changeSizeScale(m_AreaInfoPhase2.AreaSize)

                frmMain.areaPhase3.StartPoint = changePointScale(m_AreaInfoPhase3.StartPoint)
                frmMain.areaPhase3.AreaSize = changeSizeScale(m_AreaInfoPhase3.AreaSize)

                frmMain.areaPhase4.StartPoint = changePointScale(m_AreaInfoPhase4.StartPoint)
                frmMain.areaPhase4.AreaSize = changeSizeScale(m_AreaInfoPhase4.AreaSize)

                frmMain.areaPhaseCenter.StartPoint = changePointScale(m_AreaInfoPhaseCenter.StartPoint)
                frmMain.areaPhaseCenter.AreaSize = changeSizeScale(m_AreaInfoPhaseCenter.AreaSize)

                frmMain.saveAreaInfo()
                frmMain.fillAreaParam()
                frmMain.syncAreaInfo()

            End If
        End If

        frmMain.Enabled = True
    End Sub


    Public Function getMouseCurrentPos() As Point
        Dim ret As New Point

        ret.X = PointToClient(MousePosition).X - picboxSamplePic.Location.X - 1
        ret.Y = PointToClient(MousePosition).Y - picboxSamplePic.Location.Y - 1

        Return ret


    End Function





    Private Sub picboxSamplePic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picboxSamplePic.MouseDown
        IsDraft = True
        isAreaInfoChanged = True
        startPoint = getMouseCurrentPos()
    End Sub
    Public Sub picboxSamplePic_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picboxSamplePic.MouseMove

        If IsDraft Then

            Dim rect As Rectangle
            endPoint = getMouseCurrentPos()

            '1 phase
            If endPoint.X <> startPoint.X And endPoint.Y <> startPoint.Y Then
                rect.Width = Math.Abs(startPoint.X - endPoint.X)
                rect.Height = Math.Abs(startPoint.Y - endPoint.Y)
                rect.X = Math.Min(startPoint.X, endPoint.X)
                rect.Y = Math.Min(startPoint.Y, endPoint.Y)

            End If

            If radSetAreaPhase1.Checked Then
                m_AreaInfoPhase1.StartPoint = New Point(rect.X, rect.Y)
                m_AreaInfoPhase1.AreaSize = New Size(rect.Width, rect.Height)
            End If

            If radSetAreaPhase2.Checked Then
                m_AreaInfoPhase2.StartPoint = New Point(rect.X, rect.Y)
                m_AreaInfoPhase2.AreaSize = New Size(rect.Width, rect.Height)
            End If


            If radSetAreaPhase3.Checked Then
                m_AreaInfoPhase3.StartPoint = New Point(rect.X, rect.Y)
                m_AreaInfoPhase3.AreaSize = New Size(rect.Width, rect.Height)
            End If

            If radSetAreaPhase4.Checked Then
                m_AreaInfoPhase4.StartPoint = New Point(rect.X, rect.Y)
                m_AreaInfoPhase4.AreaSize = New Size(rect.Width, rect.Height)
            End If

            If radSetAreaPhaseCenter.Checked Then
                m_AreaInfoPhaseCenter.StartPoint = New Point(rect.X, rect.Y)
                m_AreaInfoPhaseCenter.AreaSize = New Size(rect.Width, rect.Height)
            End If
            'g.FillRectangle(b, startPoint.X, startPoint.Y, Math.Abs(MousePosition.X - startPoint.X), Math.Abs(MousePosition.Y - startPoint.Y))

            'g.FillRectangle(b, rect)
            reDraw()

            textMousePos.Text = "x=" & getMouseCurrentPos.X.ToString & " y=" & getMouseCurrentPos.Y.ToString & " w=" & rect.Width.ToString & " h=" & rect.Height.ToString
            textPicPos.Text = "x=" & changeScale(rect.X, ScaleX).ToString & _
                             " y=" & changeScale(rect.Y, ScaleY).ToString & _
                             " w=" & changeScale(rect.Width, ScaleX).ToString & _
                             " h=" & changeScale(rect.Height, ScaleY).ToString
        Else
            textMousePos.Text = "x=" & getMouseCurrentPos.X.ToString & " y=" & getMouseCurrentPos.Y.ToString
            textPicPos.Text = "x=" & changeScale(getMouseCurrentPos.X, ScaleX).ToString & " y=" & changeScale(getMouseCurrentPos.Y, ScaleY).ToString
        End If

    End Sub

    Public Function changeScale(ByVal inputValue As Integer, ByVal customScale As Double, Optional ByVal changeType As Integer = 0) As Integer
        changeScale = inputValue

        If changeType = 0 Then
            '控件坐标系→原始画像坐标系
            changeScale = inputValue * (1 / customScale)

        Else
            '原始画像坐标系→控件坐标系
            changeScale = inputValue * customScale

        End If


    End Function

    Public Function changePointScale(ByVal inputPoint As Point, Optional ByVal changeType As Integer = 0) As Point
        changePointScale = inputPoint


        If changeType = 0 Then
            '控件坐标系→原始画像坐标系
            changePointScale.X = inputPoint.X * (1 / ScaleX)
            changePointScale.Y = inputPoint.Y * (1 / ScaleY)


        Else
            '原始画像坐标系→控件坐标系
            changePointScale.X = inputPoint.X * ScaleX
            changePointScale.Y = inputPoint.Y * ScaleY
        End If


    End Function

    Public Function changeSizeScale(ByVal inputSize As Size, Optional ByVal changeType As Integer = 0) As Size
        changeSizeScale = inputSize

        If changeType = 0 Then
            '控件坐标系→原始画像坐标系
            changeSizeScale.Width = inputSize.Width * (1 / ScaleX)
            changeSizeScale.Height = inputSize.Height * (1 / ScaleY)


        Else
            '原始画像坐标系→控件坐标系
            changeSizeScale.Width = inputSize.Width * ScaleX
            changeSizeScale.Height = inputSize.Height * ScaleY
        End If

    End Function


    Public Sub reDraw()
        picboxSamplePic.Refresh()

        'phase1
        g.FillRectangle(b1, New Rectangle(m_AreaInfoPhase1.StartPoint, m_AreaInfoPhase1.AreaSize))

        'phase2
        g.FillRectangle(b2, New Rectangle(m_AreaInfoPhase2.StartPoint, m_AreaInfoPhase2.AreaSize))

        'phase3
        g.FillRectangle(b3, New Rectangle(m_AreaInfoPhase3.StartPoint, m_AreaInfoPhase3.AreaSize))

        'phase4
        g.FillRectangle(b4, New Rectangle(m_AreaInfoPhase4.StartPoint, m_AreaInfoPhase4.AreaSize))

        'phaseCenter
        g.FillRectangle(bCenter, New Rectangle(m_AreaInfoPhaseCenter.StartPoint, m_AreaInfoPhaseCenter.AreaSize))


    End Sub

    Private Sub picboxSamplePic_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picboxSamplePic.MouseUp
        IsDraft = False
    End Sub

    Private Sub frmAdjust_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        reDraw()
    End Sub
End Class